import { KeepAliveState } from "@/stores/interface";  // 导入KeepAlive状态类型接口

// 创建名为"useKeepAliveStore"的Pinia store
export const useKeepAliveStore = defineStore({
  id: "keepAlive",  // store的唯一标识符
  
  // 状态初始化函数
  state: (): KeepAliveState => ({
    // 存储需要保持活跃状态的组件名称数组
    keepAliveName: []  
  }),
  
  // 定义操作store的方法
  actions: {
    // 添加组件到KeepAlive缓存列表
    async addKeepAliveName(name: string) {
      // 当名称不存在于当前列表时，将其添加到数组末尾
      !this.keepAliveName.includes(name) && this.keepAliveName.push(name);
    },
    
    // 从KeepAlive缓存列表中移除组件
    async removeKeepAliveName(name: string) {
      // 过滤掉匹配名称的项，保留其他项
      this.keepAliveName = this.keepAliveName.filter(item => item !== name);
    },
    
    // 设置整个KeepAlive缓存列表（覆盖原有）
    async setKeepAliveName(keepAliveName: string[] = []) {
      // 用新数组替换当前缓存列表（空数组表示清空所有缓存）
      this.keepAliveName = keepAliveName;
    }
  }
});